PROJECTENGRAM

[PS] INTEGER OPERATIONS AND OVERFLOW

Problem Set - Integer Operations and Overflow

Tags: #◇Computer Science #◇CSARCH2 #◇Computer Architecture #◇Binary Arithmetic #◇Overflow Flag #◇Carry Flag #◇ALU #◇Two's Complement


◇Overview

In digital arithmetic and processor microarchitecture (◇ALU), integer addition and subtraction are executed on fixed-width binary words (typically 88, 1616, 3232, or 6464 bits). Because physical hardware registers possess a finite bit width nn, arithmetic operations can produce results that exceed the storage capacity of the destination register.

Microprocessors track boundary conditions by asserting specific condition status flags in the Program Status Word / Flags register:

  • Carry Flag (CC / CF): Indicates carry-out or borrow during unsigned integer arithmetic.
  • Overflow Flag (VV / OF): Indicates mathematical magnitude overflow during signed 2's complement arithmetic.
  • Sign Flag (NN / SS / SF): Mirrors the Most Significant Bit (MSB) of the result (1=negative,0=non-negative1 = \text{negative}, 0 = \text{non-negative}).
  • Zero Flag (ZZ / ZF): Asserted (11) when all bits of the result equal 00.
『』
TEXT
                            ┌─────────────────────────────────────────┐
                            │      8-bit ALU Arithmetic Result        │
                            └────────────────────┬────────────────────┘
                                                 │
                   ┌─────────────────────────────┴─────────────────────────────┐
                   ▼                                                           ▼
      ┌─────────────────────────┐                                 ┌─────────────────────────┐
      │   Unsigned Evaluation   │                                 │    Signed Evaluation    │
      │   Range: [0, 255]       │                                 │    Range: [-128, +127]  │
      └────────────┬────────────┘                                 └────────────┬────────────┘
                   │                                                           │
        ┌──────────┴──────────┐                                     ┌──────────┴──────────┐
        ▼                     ▼                                     ▼                     ▼
 ┌─────────────┐       ┌─────────────┐                       ┌─────────────┐       ┌─────────────┐
 │ C_out == 0  │       │ C_out == 1  │                       │ C_in ==     │       │ C_in !=     │
 │ (Addition)  │       │ (Addition)  │                       │ C_out       │       │ C_out       │
 │   NO OVF    │       │  OVERFLOW   │                       │   NO OVF    │       │  OVERFLOW   │
 └─────────────┘       └─────────────┘                       └─────────────┘       └─────────────┘

◇Theoretical Foundations

1. Hardware Subtraction via 2's Complement Addition

Digital subtractors avoid dedicated subtraction logic by transforming subtraction into addition using radix complement properties:
A−B=A+(−B)=A+(∼B+1)A - B = A + (-B) = A + (\sim B + 1)
where ∼B\sim B represents the bitwise inversion (1's complement) of BB, and +1+ 1 is injected via the initial carry-in (C0=1C_0 = 1) of the full-adder chain.

2. Unsigned Carry vs. Signed Overflow Rules

FeatureUnsigned Integer ArithmeticSigned (2's Complement) Arithmetic
Valid Range (n=8n=8)[0,28−1]=[0,255][0, 2^8 - 1] = [0, 255][−27,27−1]=[−128,+127][-2^7, 2^7 - 1] = [-128, +127]
Addition OverflowOccurs when true sum ≥2n\ge 2^n (Cn=1C_n = 1)Occurs when true sum ∉[−2n−1,2n−1−1]\notin [-2^{n-1}, 2^{n-1}-1]
Subtraction OverflowOccurs when A<BA < B (requires a borrow: Cn‾=1\overline{C_n} = 1)Occurs when A−B∉[−2n−1,2n−1−1]A - B \notin [-2^{n-1}, 2^{n-1}-1]
Hardware DetectionCarry Flag (CF): C=CnC = C_n (for addition)Overflow Flag (OF): V=Cn−1⊕CnV = C_{n-1} \oplus C_n
Sign-Logic FormulaN/A (operands are strictly non-negative)V=(An−1∧Bn−1∧Sn−1‾)∨(An−1‾∧Bn−1‾∧Sn−1)V = (A_{n-1} \land B_{n-1} \land \overline{S_{n-1}}) \lor (\overline{A_{n-1}} \land \overline{B_{n-1}} \land S_{n-1})

[!important] The Cin⊕CoutC_{in} \oplus C_{out} MSB Rule
In any nn-bit 2's complement adder, signed overflow occurs if and only if the carry-in into the most significant bit (Cn−1C_{n-1}) differs from the carry-out from the most significant bit (CnC_n):
V=Cn−1⊕CnV = C_{n-1} \oplus C_n


◇Problem 1: 8-Bit Binary Addition

Problem Statement

Given two 8-bit integers, perform the binary addition. The result must be represented as an 8-bit binary value. Determine whether an overflow occurs when the result is interpreted as an unsigned integer versus a signed integer.

011111112+011111112=‾201111111_2 + 01111111_2 = \underline{\hspace{4cm}}_2


Step-by-Step Solution

1. Binary Column Addition & Bitwise Carry Propagation

We set up the 8-bit full-adder chain from bit 00 (LSB) to bit 77 (MSB):

Carry-in (Ci):01111110←Carries (C8…C0)Operand A:01111111(12710=0x7F)Operand B:+01111111(12710=0x7F)Sum S:11111110(0xFE16)\begin{array}{rcccccccccl} \text{Carry-in } (C_i): & 0 & 1 & 1 & 1 & 1 & 1 & 1 & 0 & \leftarrow & \text{Carries } (C_8 \dots C_0) \\ \text{Operand } A: & & 0 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & (127_{10} = \text{0x7F}) \\ \text{Operand } B: & + & 0 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & (127_{10} = \text{0x7F}) \\ \hline \text{Sum } S: & & \mathbf{1} & \mathbf{1} & \mathbf{1} & \mathbf{1} & \mathbf{1} & \mathbf{1} & \mathbf{1} & \mathbf{0} & (\text{0xFE}_{16}) \end{array}

  • Bit-by-Bit Evaluation:

    • Bit 0: 1+1+C0(0)=2  ⟹  Sum0=0,C1=11 + 1 + C_0(0) = 2 \implies \text{Sum}_0 = 0, C_1 = 1
    • Bit 1: 1+1+C1(1)=3  ⟹  Sum1=1,C2=11 + 1 + C_1(1) = 3 \implies \text{Sum}_1 = 1, C_2 = 1
    • Bit 2: 1+1+C2(1)=3  ⟹  Sum2=1,C3=11 + 1 + C_2(1) = 3 \implies \text{Sum}_2 = 1, C_3 = 1
    • Bit 3: 1+1+C3(1)=3  ⟹  Sum3=1,C4=11 + 1 + C_3(1) = 3 \implies \text{Sum}_3 = 1, C_4 = 1
    • Bit 4: 1+1+C4(1)=3  ⟹  Sum4=1,C5=11 + 1 + C_4(1) = 3 \implies \text{Sum}_4 = 1, C_5 = 1
    • Bit 5: 1+1+C5(1)=3  ⟹  Sum5=1,C6=11 + 1 + C_5(1) = 3 \implies \text{Sum}_5 = 1, C_6 = 1
    • Bit 6: 1+1+C6(1)=3  ⟹  Sum6=1,C7=11 + 1 + C_6(1) = 3 \implies \text{Sum}_6 = 1, C_7 = 1
    • Bit 7 (MSB): 0+0+C7(1)=1  ⟹  Sum7=1,C8=00 + 0 + C_7(1) = 1 \implies \text{Sum}_7 = 1, C_8 = 0
  • Critical Boundary Carries:

    • Carry-in into MSB (C7C_7): 11
    • Carry-out from MSB (C8C_8): 00
    • Result: 11111110211111110_2 (FE16\text{FE}_{16})

2. Overflow Analysis

Perspective A: Unsigned Integer Interpretation
  • Operand Values: A=12710A = 127_{10}, B=12710B = 127_{10}
  • True Mathematical Sum: 127+127=25410127 + 127 = 254_{10}
  • 8-Bit Unsigned Dynamic Range: [0,255][0, 255]
  • Verification: Since 254≤255254 \le 255, the mathematical sum fits entirely inside the 8-bit unsigned register.
  • Hardware Flag: Carry-out C8=0  ⟹  Carry Flag C=0C_8 = 0 \implies \text{Carry Flag } C = 0.
  • Conclusion: No Overflow
Perspective B: Signed (2's Complement) Integer Interpretation
  • Operand Values: A=+12710A = +127_{10} (MSB =0=0), B=+12710B = +127_{10} (MSB =0=0)
  • True Mathematical Sum: (+127)+(+127)=+25410(+127) + (+127) = +254_{10}
  • 8-Bit Signed Dynamic Range: [−128,+127][-128, +127]
  • Verification: +254>+127+254 > +127, which exceeds the maximum positive limit of an 8-bit signed integer. The resulting bit pattern 11111110211111110_2 has MSB =1= 1, which decodes to:
    Value=−27+∑i=162i=−128+126=−210\text{Value} = -2^7 + \sum_{i=1}^6 2^i = -128 + 126 = -2_{10}
    Adding two positive integers produced an illogical negative result.
  • Hardware Flag Verification:
    V=C7⊕C8=1⊕0=1  ⟹  Overflow Flag V=1V = C_7 \oplus C_8 = 1 \oplus 0 = 1 \implies \text{Overflow Flag } V = 1
  • Conclusion: Yes (Overflow Occurred)

Solution Summary Table (Problem 1)

FieldValue
Operation011111112+01111111201111111_2 + 01111111_2
8-Bit Result11111110211111110_2
If viewed as unsigned integerNo (Sum =254∈[0,255]= 254 \in [0, 255], Cout=0C_{out} = 0)
If viewed as signed integerYes (Sum =+254∉[−128,+127]= +254 \notin [-128, +127], V=1V = 1)

◇Problem 2: 8-Bit Binary Subtraction

Problem Statement

Given two 8-bit integers, perform the binary subtraction. The result must be represented as an 8-bit binary value. Determine whether an overflow occurs when the result is interpreted as an unsigned integer versus a signed integer.

011111102−100000112=‾201111110_2 - 10000011_2 = \underline{\hspace{4cm}}_2


Step-by-Step Solution

1. Transformation to 2's Complement Addition

Let:

  • Minuend A=011111102=12610A = 01111110_2 = 126_{10}
  • Subtrahend B=100000112=13110B = 10000011_2 = 131_{10} (unsigned) or −12510-125_{10} (signed 2's complement)

To execute A−BA - B, the ALU negates BB into its 2's complement representation (−B-B) and performs addition:

  1. Bitwise Inversion (1's complement):
    ∼B=∼(100000112)=011111002\sim B = \sim(10000011_2) = 01111100_2
  2. Add 11 (2's complement):
    −B=∼B+1=011111002+1=011111012(+12510)-B = \sim B + 1 = 01111100_2 + 1 = 01111101_2 \quad (+125_{10})

2. Binary Column Addition of A+(−B)A + (-B)

Carry-in (Ci):01111100←Carries (C8…C0)Operand A:01111110(12610=0x7E)Operand (−B):+01111101(12510=0x7D)Sum S:11111011(0xFB16)\begin{array}{rcccccccccl} \text{Carry-in } (C_i): & 0 & 1 & 1 & 1 & 1 & 1 & 0 & 0 & \leftarrow & \text{Carries } (C_8 \dots C_0) \\ \text{Operand } A: & & 0 & 1 & 1 & 1 & 1 & 1 & 1 & 0 & (126_{10} = \text{0x7E}) \\ \text{Operand } (-B): & + & 0 & 1 & 1 & 1 & 1 & 1 & 0 & 1 & (125_{10} = \text{0x7D}) \\ \hline \text{Sum } S: & & \mathbf{1} & \mathbf{1} & \mathbf{1} & \mathbf{1} & \mathbf{1} & \mathbf{0} & \mathbf{1} & \mathbf{1} & (\text{0xFB}_{16}) \end{array}

  • Bit-by-Bit Evaluation:

    • Bit 0: 0+1+C0(0)=1  ⟹  Sum0=1,C1=00 + 1 + C_0(0) = 1 \implies \text{Sum}_0 = 1, C_1 = 0
    • Bit 1: 1+0+C1(0)=1  ⟹  Sum1=1,C2=01 + 0 + C_1(0) = 1 \implies \text{Sum}_1 = 1, C_2 = 0
    • Bit 2: 1+1+C2(0)=2  ⟹  Sum2=0,C3=11 + 1 + C_2(0) = 2 \implies \text{Sum}_2 = 0, C_3 = 1
    • Bit 3: 1+1+C3(1)=3  ⟹  Sum3=1,C4=11 + 1 + C_3(1) = 3 \implies \text{Sum}_3 = 1, C_4 = 1
    • Bit 4: 1+1+C4(1)=3  ⟹  Sum4=1,C5=11 + 1 + C_4(1) = 3 \implies \text{Sum}_4 = 1, C_5 = 1
    • Bit 5: 1+1+C5(1)=3  ⟹  Sum5=1,C6=11 + 1 + C_5(1) = 3 \implies \text{Sum}_5 = 1, C_6 = 1
    • Bit 6: 1+1+C6(1)=3  ⟹  Sum6=1,C7=11 + 1 + C_6(1) = 3 \implies \text{Sum}_6 = 1, C_7 = 1
    • Bit 7 (MSB): 0+0+C7(1)=1  ⟹  Sum7=1,C8=00 + 0 + C_7(1) = 1 \implies \text{Sum}_7 = 1, C_8 = 0
  • Critical Boundary Carries:

    • Carry-in into MSB (C7C_7): 11
    • Carry-out from MSB (C8C_8): 00
    • Result: 11111011211111011_2 (FB16\text{FB}_{16})

3. Overflow Analysis

Perspective A: Unsigned Integer Interpretation
  • Operand Values: A=12610A = 126_{10}, B=13110B = 131_{10}
  • True Mathematical Difference: 126−131=−510126 - 131 = -5_{10}
  • 8-Bit Unsigned Dynamic Range: [0,255][0, 255]
  • Underflow / Borrow Detection:
    • Because −5<0-5 < 0, a negative value cannot be represented as an unsigned integer.
    • In unsigned subtraction A−BA - B, an underflow (borrow) occurs whenever A<BA < B.
    • Hardware mapping: Borrow=C8‾=1−C8\text{Borrow} = \overline{C_8} = 1 - C_8.
    • Since C8=0C_8 = 0, Borrow=1\text{Borrow} = 1, indicating an unsigned overflow/underflow condition. The resulting bit pattern 11111011211111011_2 evaluates to 25110251_{10} (256−5256 - 5).
  • Conclusion: Yes (Overflow / Borrow Occurred)
Perspective B: Signed (2's Complement) Integer Interpretation
  • Operand Values:
    • A=011111102=+12610A = 01111110_2 = +126_{10} (positive)
    • B=100000112=−12510B = 10000011_2 = -125_{10} (negative, because ∼(10000011)+1=011111012=125\sim(10000011) + 1 = 01111101_2 = 125)
  • True Mathematical Difference:
    A−B=(+126)−(−125)=+126+125=+25110A - B = (+126) - (-125) = +126 + 125 = +251_{10}
  • 8-Bit Signed Dynamic Range: [−128,+127][-128, +127]
  • Verification:
    • +251>+127+251 > +127, which exceeds the maximum positive 8-bit limit.
    • Subtracting a negative integer from a positive integer (Positive−Negative)(\text{Positive} - \text{Negative}) mathematically must yield a positive integer.
    • The resulting 8-bit pattern 11111011211111011_2 has MSB =1= 1, yielding −510-5_{10} in 2's complement.
  • Hardware Flag Verification:
    V=C7⊕C8=1⊕0=1  ⟹  Overflow Flag V=1V = C_7 \oplus C_8 = 1 \oplus 0 = 1 \implies \text{Overflow Flag } V = 1
  • Conclusion: Yes (Overflow Occurred)

Solution Summary Table (Problem 2)

FieldValue
Operation011111102−10000011201111110_2 - 10000011_2
8-Bit Result11111011211111011_2
If viewed as unsigned integerYes (Difference =−5∉[0,255]= -5 \notin [0, 255], Borrow=Cout‾=1\text{Borrow} = \overline{C_{out}} = 1)
If viewed as signed integerYes (Difference =+251∉[−128,+127]= +251 \notin [-128, +127], V=1V = 1)

◇Key Takeaways & Master Reference Guide

Flag Computation Matrix (nn-bit Arithmetic)

『』
TEXT
                            ┌──────────────────────────────────────────────┐
                            │           ALU Status Flag Formulas           │
                            ├──────────────────────────────────────────────┤
                            │ Zero Flag:      Z = ~(S_0 | S_1 | ... | S_7) │
                            │ Sign Flag:      S = S_7                      │
                            │ Carry Flag:     C = C_8 (Add) or ~C_8 (Sub)  │
                            │ Overflow Flag:  V = C_7 ^ C_8                │
                            └──────────────────────────────────────────────┘

Unsigned vs. Signed Overflow Quick-Check Rules

  1. Unsigned Arithmetic:

    • Addition (A+BA + B): Overflow occurs   ⟺  Cout=1\iff C_{out} = 1 (True Sum≥2n\text{True Sum} \ge 2^n).
    • Subtraction (A−BA - B): Overflow (Borrow) occurs   ⟺  Cout=0\iff C_{out} = 0 (A<BA < B, resulting in negative difference).
  2. Signed 2's Complement Arithmetic:

    • Addition (A+BA + B):
  • (Positive+Positive=Negative)  ⟹  Overflow (V=1)(\text{Positive} + \text{Positive} = \text{Negative}) \implies \text{Overflow } (V=1)
  • (Negative+Negative=Positive)  ⟹  Overflow (V=1)(\text{Negative} + \text{Negative} = \text{Positive}) \implies \text{Overflow } (V=1)
  • (Positive+Negative)  ⟹  Overflow Impossible (V=0)(\text{Positive} + \text{Negative}) \implies \text{Overflow Impossible } (V=0)
    • Subtraction (A−BA - B):
  • (Positive−Negative=Negative)  ⟹  Overflow (V=1)(\text{Positive} - \text{Negative} = \text{Negative}) \implies \text{Overflow } (V=1)
  • (Negative−Positive=Positive)  ⟹  Overflow (V=1)(\text{Negative} - \text{Positive} = \text{Positive}) \implies \text{Overflow } (V=1)
  • (Same Sign−Same Sign)  ⟹  Overflow Impossible (V=0)(\text{Same Sign} - \text{Same Sign}) \implies \text{Overflow Impossible } (V=0)
    • Universal Hardware Check:
      V=Cin (MSB)⊕Cout (MSB)V = C_{in\text{ (MSB)}} \oplus C_{out\text{ (MSB)}}

◇References & Navigation

[PS] Integer Operations and Overflow | PROJECT ENGRAM